package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;

/**
 * Controller class for the loans.
 * 
 * @author (Alexander Nikolaj Bach Pedersen) 
 * @version (17-04-2013)
 */
public class LoanCtr
{
    // instance variables - replace the example below with your own
    private LoanContainer loanContainer;
    private AddressCtr addressCtr;
    private DVDCtr dvdCtr;
    //private Loan loan;
    

    /**
     * Constructor for objects of class LoanCtr
     */
    public LoanCtr()
    {
        // initialise instance variables
        loanContainer = LoanContainer.getInstance();
    }

    /**
     * Returns a Person object by entering the decired ID.
     * 
     * @param id    the id of a Person object.
     * @return      a Person object.
     */
    public Person getPerson(int id)
    {
        return addressCtr.getPerson(id);
    }

    /**
     * Returns a Copy object by entering the decired dvdId and serial.
     * 
     * @param dvdId     the ID of a dvd object.
     * @param serial    the serial of a copy object.
     * @return          a Copy object.
     */
    public Copy getCopy(int dvdId, int serial)
    {
        return dvdCtr.getDVD(dvdId).getCopy(serial);
    }

    /**
     * Is adding a new Loan object with the objects Person and Copy, and the fields
     * date and period to the loanContainer. Is also adding a status which equals 0,
     * and an id equals 0.
     * 
     * @param person    a Person object.
     * @param copy      a Copy object.
     * @param date      the date when the Loan is created.
     * @param period    the amount of days before the loan is expired.
     * @return          the newly created Loan object.
     */
    public Loan createLoan(Person person, Copy copy, String date, String period)
    {
        Loan loan = new Loan(person, copy, 0, date, period, 0);
        return loanContainer.addLoan(loan);
    }
    
    /**
     * Returns a Loan object by entering the decired Loan id. The return is made
     * through the loanContainer which returns the object with the entered id.
     * 
     * @param id        the id of a Loan object.
     * @return          a Loan object.
     */
    public Loan getLoan(int id)
    {
        return loanContainer.getLoan(id);
    }
    
    /**
     * Returns an ArrayList containing all active loans by a given Person object p.
     * The method is using a for-each loop to go through objects in the listActiveLoans
     * method in the loanContainer class.
     * If the Person object on a loan is equal to the one entered, and the status of
     * the loan is 0, the loan object is added to a new ArrayList calles activeLoans.
     * 
     * @param p         a Person object.
     * @param status    the status of the loan objects to be returned
     * @return          an ArrayList with active loans on the entered Person object.
     * 
     * Edit Jan, Added Status int to header so we can make list from any status
     */
    public ArrayList<Loan> listLoans(Person p, int status)
    {
        ArrayList<Loan> activeLoans = new ArrayList<Loan>();
        
        //for(int index = 0; index < loanContainer.listActiveLoans().size(); index++)
        for(Loan l : loanContainer.listActiveLoans())
        {
            //Loan loan = loanContainer.listActiveLoans().get(index);
            if(l.getPerson().equals(p) && l.getStatus() == status)
            {
                activeLoans.add(l);
            }
        }
        return activeLoans;
    }
}
	